/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.edu.fatec.ads.facade;

import br.edu.fatec.ads.bo.BusinessObjectCategoria;
import br.edu.fatec.ads.classes.Categoria;
import br.edu.fatec.ads.util.ComboBoxItem;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.hibernate.HibernateException;

/**
 *
 * @author Ubirajara
 */
public class FacadeCategoria {

    private BusinessObjectCategoria regraNegocio = null;
    private static FacadeCategoria singleton = null;

    private FacadeCategoria() {
        regraNegocio = new BusinessObjectCategoria();
    }

    public static FacadeCategoria getInstance(){
        if(singleton == null){
            singleton = new FacadeCategoria();
        }

        return singleton;
    }

    public List<Categoria> recuperarTodasCategorias()
        throws HibernateException{
        List<Categoria> categorias = Collections.EMPTY_LIST;

        regraNegocio.beginTransaction();

        try{
            categorias = regraNegocio.recuperarTodasCategorias();

            regraNegocio.commitTransaction();
        }catch(HibernateException cause){
            cause.printStackTrace();

            regraNegocio.rollbackTransaction();
        }

        return categorias;
    }

    public List<ComboBoxItem> recuperarCategorias(){
        List<ComboBoxItem> comboItems = new ArrayList<ComboBoxItem>();
        List<Categoria> categorias = this.recuperarTodasCategorias();

        for (Categoria categoria : categorias) {
            comboItems
                .add(new ComboBoxItem
                            (categoria.getCodigo(),
                                    categoria.getDescricao()));
        }

        return comboItems;
    }

}